<?php
namespace app\item;

use ArrayObject;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

class Record
{
    private $io_type = [
        1 => '支出',
        2 => '收入',
        3 => '资金转移'
    ];
    private $record_type = [
        'alipay' => '支付宝',
        'wechat' => '微信支付',
    ];
    private $file_types = [
        'Xlsx',
        'Xml',
        'Ods',
        'Slk',
        'Gnumeric',
        'Csv',
    ];
    private $file_name = '';
    private $file_type = '';
    public function __construct(String $name)
    {
        $file_suffix = ucfirst(pathinfo($name, PATHINFO_EXTENSION));
        if (!array_search($file_suffix, $this->file_types)) {
            throw new \Exception("请导入正确文件类型： Xlsx,Xml,Ods,Slk,Gnumeric,Csv");
        }
        $this->file_name = $name;
        $this->file_type = $file_suffix;
    }
    
    private function alipay(Array $data)
    {
        $res = [];
        foreach ($data as $key => $value) {
            $str = '';
            foreach ($value as $k => $v) {
                if (!empty($v)) {
                    $str .= $v;
                }
            }
            $res[$key] = stristr($str, ',') ? explode(',', trim($str)) : trim($str);
        }

        $res = array_slice($res, 0, count($res)-7);
        $lists = [];
        foreach ($res as $key => $value) {
            // 支付宝
            if (is_array($value)) {
                $io = array_search(trim($value[10]), $this->io_type) ? 
                        array_search(trim($value[10]), $this->io_type) : 
                        array_search(trim($value[15]), $this->io_type);
                // 交易成功则写入数据
                if ($io) {
                    $itme = [
                        // 收支（收入与支出）
                        'io' => $io,
                        // 金额
                        'price' => $value[9],
                        // 描述
                        'desc' => trim($value[8]),
                        // 标题
                        'title' => trim($value[7]),
                        // 交易时间
                        'pay_time' => trim($value[4]),
                        // 交易日期
                        'pay_date' => date('Y-m-d', strtotime(trim($value[4]))),
                    ];
                    array_push($lists, $itme);
                }   
            }
        }
        return $lists;
    }

    private function wechat(Array $data)
    {        
        foreach ($data as $key => $value) {
            foreach ($value as $k => $v) {
                if (stristr($v, '¥')) {       
                    $value[$k] = str_replace('¥', '', str_replace(',', '', $v));
                }
            }
            $str = implode(',', $value);
            $res[$key] = stristr($str, ',') ? explode(',', trim($str)) : trim($str);
        }

        $res = array_slice($res, 17, count($res)-1);
        $lists = [];
        foreach ($res as $key => $value) {
            // 微信
            if (is_array($value)) {
                $io = array_search(trim($value[4]), $this->io_type) ? 
                        array_search(trim($value[4]), $this->io_type) : 
                        3;
                // 交易成功则写入数据
                if ($io) {
                    $itme = [
                        // 收支（收入与支出）
                        'io' => $io,
                        // 金额
                        'price' => $value[5],
                        // 描述
                        'desc' => trim($value[3]),
                        // 标题
                        'title' => trim($value[2]),
                        // 交易时间
                        'pay_time' => trim($value[0]),
                        // 交易日期
                        'pay_date' => date('Y-m-d', strtotime(trim($value[0]))),
                    ];
                    array_push($lists, $itme);
                }

            }

        }

        return $lists;
    }

    public function run()
    {
        // 建立表格阅读器
        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($this->file_type);
        if (stristr($this->file_name, 'alipay')) {
            $reader->setInputEncoding('GBK');
        }
        
        // 加载表格
        $spreadsheet = $reader->load($this->file_name);
        $data = $spreadsheet
            ->getSheet(0) // 指定第一个工作表为当前
            ->toArray();  // 转为数组
        
        // 账单类型
        // $record_type = [
        //     'alipay' => '支付宝',
        //     'wechat' => '微信支付',
        // ];
        if (isset($data[0][0])) {
            foreach ($this->record_type as $k => $v) {
                if (stristr($data[0][0], $v)) {
                    $type = $k;
                    $data = $this->$type($data);
                    break;
                }
            }
        }

        return $data ?? [];
    }

    public static function aliin()
    {
        $inputFileName = './public/alipay_record_20190324_1548_1.csv';
        // $inputFileName = './public/微信支付账单(20191006-20191221)(1).csv';
        $inputFileType = 'Csv';
        // 建立表格阅读器
        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
        if (stristr($inputFileName, 'alipay')) {
            $reader->setInputEncoding('GBK');
        }
        
        // 加载表格
        $spreadsheet = $reader->load($inputFileName);
        $data = $spreadsheet
            ->getSheet(0) // 指定第一个工作表为当前
            ->toArray();  // 转为数组
        
        // 账单类型
        $record_type = [
            'alipay' => '支付宝',
            'wechat' => '微信支付',
        ];
        if (isset($data[0][0])) {
            foreach ($this->record_type as $k => $v) {
                if (stristr($data[0][0], $v)) {
                    $type = $k;
                    $data = $this->$type($data);
                    break;
                }
            }
        }

        return $data ?? [];
    }    
}